<?php
/**
 * Created by PhpStorm.
 * User: HP_USER
 * Date: 2019/1/7
 * Time: 10:42
 */

namespace app\data;

use app\common\Common;
use think\Db;

class OriginalDate extends Base
{

    public function showOriginalMemberInfo($user_id)
    {
        return Db::table('drh_original_member a')
            ->join('drh_user_info b', 'a.user_id=b.id', 'left')
            ->field([
                'a.id original_id',
                'a.user_name original_name',
                'a.integral ',
                'a.recommend_integral',
                'a.user_id',
                'b.user_name',
                'b.user_head_img',
                'a.end_time',
                'a.verify_status',
                'a.withdraw_integral',
                'a.dividend_integral',
                'a.img_signature',
                'a.img_positive',
                'a.append_integral',
                'a.identity'
            ])->where([
                'a.user_id' => $user_id,
                'a.verify_status' => 1,
                'a.recover_status' => 0
            ])->find();
    }

    public function getRecommendOriginalMemberNumber($user_id)
    {
        $res = Db::table('drh_user_friend a')
            ->join('drh_original_member b', 'a.friend_id=b.user_id', 'left')
            ->field([
                'count(*) count'
            ])->where([
                'a.user_id' => $user_id,
                'b.verify_status' => 1,
                'b.recover_status' => 0
            ])->find();
        return empty($res) ? 0 : $res['count'];
    }

    public function addOriginalMember($user_id, $user_name, $card_number, $time, $img_positive, $img_back, $img_signature, $integral)
    {
        return Db::table('drh_original_member')
            ->insertGetId([
                'user_id' => $user_id,
                'user_name' => $user_name,
                'id_card' => $card_number,
                'img_positive' => $img_positive,
                'img_back' => $img_back,
                'img_signature' => $img_signature,
                'create_time' => $time,
                'integral' => $integral,
            ]);
    }

    public function editOriginalMember($user_id, $user_name, $card_number, $time, $img_positive, $img_back, $img_signature, $integral)
    {
        return Db::table('drh_original_member')
            ->where([
                'user_id' => $user_id,
            ])
            ->update([
                'user_name' => $user_name,
                'verify_status' => 0,
                'id_card' => $card_number,
                'img_positive' => $img_positive,
                'img_back' => $img_back,
                'img_signature' => $img_signature,
                'create_time' => $time,
                'integral' => $integral,
            ]);
    }

    public function getLastQuitOriginalApply($user_id)
    {
        return Db::table('drh_quit_original')
            ->field([
                'verify_status',
            ])->where([
                'user_id' => $user_id
            ])->order("create_time desc")
            ->find();
    }

    public function appendOriginalIntegral($user_id, $original_id, $user_name, $card_number, $img_positive,
                                           $img_back, $img_signature, $integral, $time, $order_num)
    {
        return Db::table('drh_original_append')
            ->insertGetId([
                'user_id' => $user_id,
                'original_id' => $original_id,
                'order_num' => $order_num,
                'user_name' => $user_name,
                'append_integral' => $integral,
                'id_card' => $card_number,
                'img_positive' => $img_positive,
                'img_back' => $img_back,
                'img_signature' => $img_signature,
                'create_time' => $time
            ]);
    }

    public function checkOriginalMemberStatus($user_id)
    {
        return Db::table('drh_original_member a')
            ->join('drh_user_info b', 'a.user_id=b.id', 'left')
            ->field([
                'a.user_id',
                'a.user_name',
                'a.id_card',
                'a.img_positive',
                'a.verify_status',
                'a.recover_status',
                'a.integral',
                'a.recommend_integral',
                'a.append_integral',
                'a.img_signature',
                'a.create_time',
                'a.end_time',
                'a.end_time',
                'a.msg',
            ])->where([
                'a.user_id' => $user_id,
                'a.verify_status' => 1,
                'a.recover_status' => 0,
                'b.recover_status' => 0
            ])->find();
    }

    public function getOriginalInfo($user_id)
    {
        return Db::table('drh_original_member')
            ->where([
                'user_id' => $user_id
            ])->find();
    }

    public function quitOriginalMember($order_num, $time, $user_id, $info,
                                       $user_name, $card_number, $img_positive,
                                       $img_back, $img_signature)
    {
        return Db::table('drh_quit_original')
            ->insertGetId([
                'order_num' => $order_num,
                'user_id' => $user_id,
                'original_info' => $info,
                'create_time' => $time,
                'user_name' => $user_name,
                'id_card' => $card_number,
                'img_positive' => $img_positive,
                'img_back' => $img_back,
                'img_signature' => $img_signature
            ]);
    }

    public function checkOriginalPledgeIntegral($original_id)
    {
        return Db::table('drh_original_pledge_integral')
            ->field([
                'verify_status',
                'end_time',
                'msg',
                'order_num',
            ])->where([
                'original_id' => $original_id,
            ])->order("create_time desc")
            ->find();
    }

    public function originalPledgeIntegral($user_id, $original_id, $user_name, $card_number, $img_positive,
                                           $img_back, $img_signature, $time, $order_num)
    {
        return Db::table('drh_original_pledge_integral')
            ->insertGetId([
                'order_num' => $order_num,
                'user_id' => $user_id,
                'original_id' => $original_id,
                'create_time' => $time,
                'user_name' => $user_name,
                'id_card' => $card_number,
                'img_positive' => $img_positive,
                'img_back' => $img_back,
                'img_signature' => $img_signature
            ]);
    }

    public function checkPledgeOrder($user_id, $p_order_num)
    {
        return Db::table('drh_original_pledge_ransom_integral a')
            ->join('drh_original_pledge_integral b', 'a.p_order_num=b.order_num', 'left')
            ->field([
                'a.verify_status',
                'b.end_time'
            ])->where([
                'a.user_id' => $user_id,
                'a.p_order_num' => $p_order_num,
                'b.type_status' => 0,
                'b.verify_status' => 1
            ])
            ->order("a.create_time desc")
            ->find();

    }

    public function originalPledgeRansomIntegral($user_id, $original_id, $user_name, $card_number, $img_positive,
                                                 $img_back, $img_signature, $time, $order_num, $p_order_num)
    {
        return Db::table('drh_original_pledge_ransom_integral')
            ->insertGetId([
                'order_num' => $order_num,
                'p_order_num' => $p_order_num,
                'user_id' => $user_id,
                'original_id' => $original_id,
                'user_name' => $user_name,
                'id_card' => $card_number,
                'img_positive' => $img_positive,
                'img_back' => $img_back,
                'img_signature' => $img_signature,
                'create_time' => $time
            ]);
    }

    public function getOriginalUnRecommend()
    {
        return Db::table('drh_original_member')
            ->field([
                'user_id',
                'integral',
                'append_integral',
                'end_time'
            ])->where([
                'status' => 0,
                'verify_status' => 1,
                'recover_status' => 0
            ])->select();
    }

    public function getSuperiorOriginalInfo($user_id)
    {
        return Db::table('drh_user_friend a')
            ->join('drh_original_member b', 'a.user_id=b.user_id', 'left')
            ->field([
                'b.user_id',
                'b.recommend_integral'
            ])->where([
                'a.friend_id' => $user_id,
                'b.verify_status' => 1,
                'b.recover_status' => 0
            ])->find();
    }

    public function updateRecommendIntegral($recommend_integral, $user_id)
    {
        return Db::table('drh_original_member')
            ->where([
                'user_id' => $user_id
            ])->update([
                'recommend_integral' => $recommend_integral
            ]);
    }

    public function updateOriginalRecommendStatus($user_id, $msg)
    {
        return Db::table('drh_original_member')
            ->where([
                'user_id' => $user_id
            ])->update([
                'status' => 1,
                'msg' => $msg
            ]);
    }

    public function geiPUserInfo($user_id)
    {
        return Db::table('drh_user_friend a')
            ->join('drh_user_info b', 'a.user_id=b.id', 'left')
            ->join('drh_original_member c', 'a.user_id=c.user_id', 'left')
            ->join('drh_shop e', 'a.user_id=e.user_id', 'left')
            ->field([
                'b.id p_id',
                'b.user_head_img',
                'b.user_name',
                'b.user_tel',
                'IFNULL(c.id,0) original_id',
                '(c.integral+c.append_integral) integral',
                'c.recommend_integral',
                'IFNULL(e.id,0) shop_id',
            ])->where([
                'a.friend_id' => $user_id,
                'b.recover_status' => 0,
                'c.verify_status' => 1,
                'c.recover_status' => 0
            ])->find();
    }

    public function geUserInfo($user_id)
    {
        return Db::table('drh_user_info a')
            ->join('drh_original_member c', 'a.id=c.user_id', 'left')
            ->join('drh_shop e', 'a.id=e.user_id', 'left')
            ->field([
                'a.id user_id',
                'a.user_head_img',
                'a.user_name',
                'a.user_tel',
                'IFNULL(c.id,0) original_id',
                '(c.integral+c.append_integral) integral',
                'c.recommend_integral',
                'IFNULL(e.id,0) shop_id',
            ])->where([
                'a.id' => $user_id,
                'a.recover_status' => 0,
                'c.verify_status' => 1,
                'c.recover_status' => 0
            ])->find();
    }

    public function geNUserInfo($user_id, $page, $sort, $pre)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, 'a.');
        return Db::table('drh_user_friend a')
            ->join('drh_user_info b', 'a.friend_id=b.id', 'left')
            ->join('drh_original_member c', 'a.friend_id=c.user_id', 'left')
            ->join('drh_shop e', 'b.id=e.user_id', 'left')
            ->field([
                'b.id n_id',
                'b.user_head_img',
                'b.user_name',
                'b.user_tel',
                'IFNULL(c.id,0) original_id',
                '(c.integral+c.append_integral) integral',
                'c.recommend_integral',
                'IFNULL(e.id,0) shop_id',
            ])->where([
                'a.user_id' => $user_id,
                'b.recover_status' => 0,
                'c.verify_status' => 1,
                'c.recover_status' => 0
            ])->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function getPledgeRansomIntegralStatus($user_id)
    {
        return Db::table('drh_original_pledge_integral')
            ->field([
                'order_num',
                'verify_status',
                'type_status'
            ])->where([
                'user_id' => $user_id,
            ])->order("create_time desc")
            ->find();
    }

    public function checkOriginalRecommendIntegral($original_id)
    {
        return Db::table('drh_original_recommend_integral_withdraw')
            ->field([
                'verify_status',
                'end_time',
                'msg',
                'order_num',
            ])->where([
                'original_id' => $original_id,
            ])->order("create_time desc")
            ->find();
    }

    public function originalRecommendIntegral($user_id, $original_id, $user_name, $card_number, $img_positive,
                                              $img_back, $img_signature, $time, $order_num, $money)
    {
        return Db::table('drh_original_recommend_integral_withdraw')
            ->insertGetId([
                'order_num' => $order_num,
                'user_id' => $user_id,
                'original_id' => $original_id,
                'create_time' => $time,
                'user_name' => $user_name,
                'id_card' => $card_number,
                'img_positive' => $img_positive,
                'img_back' => $img_back,
                'img_signature' => $img_signature,
                'money' => $money
            ]);
    }

    public function originalRecommendIntegralChange($user_id, $original_id, $money)
    {
        return Db::table('drh_original_member')
            ->where([
                'user_id' => $user_id,
                'id' => $original_id
            ])->update([
                'recommend_integral' => $money
            ]);
    }

    public function originalDividendIntegral($user_id, $original_id, $user_name, $card_number, $img_positive,
                                             $img_back, $img_signature, $time, $order_num, $money)
    {
        return Db::table('drh_original_dividend_integral_withdraw')
            ->insertGetId([
                'order_num' => $order_num,
                'user_id' => $user_id,
                'original_id' => $original_id,
                'create_time' => $time,
                'user_name' => $user_name,
                'id_card' => $card_number,
                'img_positive' => $img_positive,
                'img_back' => $img_back,
                'img_signature' => $img_signature,
                'money' => $money
            ]);
    }

    public function originalDividendIntegralChange($user_id, $original_id, $money)
    {
        return Db::table('drh_original_member')
            ->where([
                'user_id' => $user_id,
                'id' => $original_id
            ])->update([
                'dividend_integral' => $money
            ]);
    }
}